#include "socket.hpp"

int Connectto(Socket & s)
{
	if (connect(s.sock, (sockaddr*)&s.socket_addr, sizeof(sockaddr)) < 0)
	{
		printf("%s Connect Error\n", s.socket_name.c_str());
		return -1;
	}
	printf("connect to %s succeed\n", s.socket_name.c_str());
	return 0;
}

int Sendto(const Socket& s, const char* sendData, int len)
{
	int sendlen;
	try
	{
		sendlen = send(s.sock, sendData, len, 0);
	}
	catch (std::exception e)
	{
		printf("Send to %s Error %s \n", s.socket_name.c_str(), e.what());
		return -1;
	}
	if (sendlen < 0)
	{
		printf("Send to %s Error \n", s.socket_name.c_str());
	}
	else
	{
		//printf("Send to %s:\t %d \t Bytes Data\n", s.socket_name.c_str(), sendlen);
	}
	return sendlen;
}

int Recvfrom(const Socket& s, char* recvData, int maxlen)
{
	int recvlen;
	try
	{
		recvlen = recv(s.sock, recvData, maxlen, 0);
	}
	catch (std::exception e)
	{
		printf("Recv from %s Error %s \n", s.socket_name.c_str(), e.what());
		return -1;
	}

	if (recvlen < 0)
	{
		printf("Recv from %s Error\n", s.socket_name.c_str());
	}
	else
	{
		//printf("Recv from %s:\t %d \t Bytes Data\n", s.socket_name.c_str(), recvlen);
	}
	
	return recvlen;
}